﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PersonBlog.Model;
using PersonBlog.BLL;
using PersonBlog.Models;
using System.Data;
using PersonBlog.Common;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json;
using System.Text;
using PWDManage.Model.QueryParam;

namespace PersonBlog.Controllers
{
    public class AgendaManageController : Controller
    {
        AgendaManageBLL amBll = new AgendaManageBLL();

        // GET: AgendaManage
        public ActionResult Index()
        {
            AgendaManageEntity schedule = new AgendaManageEntity();
            ViewBag.Schedule = schedule;

            //绑定所有日程
            DataSet ds = amBll.GetAllList();
            List<AgendaManageEntity> list = Assistant.DataSetToEntityList<AgendaManageEntity>(ds, 0).ToList();
            StringBuilder sb = new StringBuilder();
            
            foreach (AgendaManageEntity item in list)
            {
                sb.Append("{ \"title\":\""+item.Topic+ "\" ,\"start\":\"" + item.StartTime.Value.ToString("yyyy-MM-dd")+ "\",\"end\":\"" + item.EndTime.Value.ToString("yyyy-MM-dd")+ "\",\"backgroundColor\":\"#25992E\"},");
            }
            string result = string.Format(@"{0}", sb.ToString().TrimEnd(','));
            ViewBag.EventList= result;

            return View();
        }

        public ActionResult List()
        {
            QueryParamBase query = new QueryParamBase();
            query.PageCount = 1;
            query.PageSize = 10;

            int Total = 0;
            //绑定所有日程
            DataSet ds = amBll.GetListByPaging(query, out Total);
            List<AgendaManageEntity> list = new List<AgendaManageEntity>();
            if (ds.Tables.Count > 0)
            {
                list = Assistant.DataSetToEntityList<AgendaManageEntity>(ds, 0).ToList();
            }
            ViewBag.AgendaList = list;

            return View(query);
        }

        [HttpPost]
        public ActionResult List(QueryParamBase query)
        {
            int Total = 0;
            //绑定所有日程
            DataSet ds = amBll.GetListByPaging(query, out Total);
            List<AgendaManageEntity> list = new List<AgendaManageEntity>();
            if (ds.Tables.Count>0)
            {
                list = Assistant.DataSetToEntityList<AgendaManageEntity>(ds, 0).ToList();
            }
           
            ViewBag.AgendaList = list;

            return View(query);
        }

       
        // GET: AgendaManage/Details/5
        public ActionResult Details(int id)
        {
            return View();
        }

        // GET: AgendaManage/Create
        public ActionResult Create()
        {
            AgendaManageEntity ame = new AgendaManageEntity();
            ame.StartTime = DateTime.Now;
            ame.EndTime = DateTime.Now.AddDays(1);
            return View(ame);
        }

        // POST: AgendaManage/Create
        [HttpPost]
        public ActionResult Create(AgendaManageEntity ame)
        {
            try
            {
                // TODO: Add insert logic here
                ame.CreateTime = DateTime.Now;
                if (amBll.Add(ame)>=0)
                {

                }
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        

        // GET: AgendaManage/Delete/5
        public ActionResult Delete(int id)
        {
            return View();
        }

        // POST: AgendaManage/Delete/5
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        [HttpPost]
        public ActionResult _AddSchedule(AgendaManageEntity Schedule)
        {
            ViewBag.Model = Schedule;

            Result r = new Result();
            if (string.IsNullOrWhiteSpace(Schedule.Topic))
            {
                r.isSuccess = false;
                r.Message = "主题不能为空！";
                ViewBag.Result = r;
                return View();
            }
            if (Schedule.StartTime == null)
            {
                r.isSuccess = false;
                r.Message = "开始时间不能为空！";
                ViewBag.Result = r;
                return View();
            }
            if (Schedule.EndTime == null)
            {
                r.isSuccess = false;
                r.Message = "结束时间不能为空！";
                ViewBag.Result = r;
                return View();
            }
            Schedule.CreateTime = DateTime.Now;
            amBll.Add(Schedule);

            return RedirectToAction("Index");
        }
    }
}
